package com.css.dzj.zfjg.modules.other.service.impl;

import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.CvaConstants;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.common.util.SnowflakeId;
import com.css.dzj.zfjg.modules.jcdx.entity.JgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.jcdx.entity.QJgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.other.entity.JgJgxwOtherActionEntity;
import com.css.dzj.zfjg.modules.other.entity.QJgJgxwOtherActionEntity;
import com.css.dzj.zfjg.modules.other.mapper.JgJgxwOtherActionMapper;
import com.css.dzj.zfjg.modules.other.service.JgJgxwOtherActionService;
import com.css.dzj.zfjg.modules.taskinfo.vo.rwjgLh;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@Service
public class JgJgxwOtherActionServiceImpl implements JgJgxwOtherActionService {
    @Autowired
    private JgJgxwOtherActionMapper otherActionMapper;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private HttpServletRequest request;


    @Override
    public QueryResults<JgJgxwOtherActionEntity> queryPage(Pageable pageable) {
        QJgJgxwOtherActionEntity q = QJgJgxwOtherActionEntity.jgJgxwOtherActionEntity;
        Predicate predicate = q.isNotNull().or(q.isNull());
        /**
         * 查询条件
         */
        //predicate = (StringUtils.isEmpty(objectname)) ? predicate : ExpressionUtils.and(predicate, q.objectname.like("%" + objectname + "%"));
        // 返回结果
        return jpaQueryFactory.selectFrom(q).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }

    @Override
    public JgJgxwOtherActionEntity getById(String id) {
        return otherActionMapper.getOne(id);
    }

    @Override
    public void save(JgJgxwOtherActionEntity otherActionEntity) {
        /**
         * 设置默认值
         */
        Date now = Calendar.getInstance().getTime();
        otherActionEntity.setCdTime(now);
        //cd_id
        otherActionEntity.setCdId(SnowflakeId.getId());
        //用户字段
        otherActionEntity.setCurrentUserId(ServiceUtil.getUserId(request));
        //唯一标识
        otherActionEntity.setRecordUniqueIdentity(SnowflakeId.getId());
        //新增数据类型i-insert,u-update,d-delete
        otherActionEntity.setCdOperation(CvaConstants.INSERT_OPER_FLAG);
        //创建时间
        otherActionEntity.setCreateTime(now);
        //创建人
        if(StringUtils.isNotEmpty(ServiceUtil.getUserId(request))){
            otherActionEntity.setCreateUser(ServiceUtil.getUserId(request));
        }
        //状态
        otherActionEntity.setFlag(CvaConstants.UN_DELETED_FLAG);
        //来源
        otherActionEntity.setCdSource(CvaConstants.CD_SOURCE);

        otherActionMapper.save(otherActionEntity);
    }

    @Override
    public void updateById(JgJgxwOtherActionEntity otherActionEntity) {
        /**
         * update设置默认值
         */
        Date now = Calendar.getInstance().getTime();
        otherActionEntity.setGxsj(now);
        //新增数据类型i-insert,u-update,d-delete
        otherActionEntity.setCdOperation(CvaConstants.UPDATE_OPER_FLAG);
        //行政区划代码
        otherActionEntity.setAreaNumber("123456");
        //状态
        otherActionEntity.setFlag(CvaConstants.UN_DELETED_FLAG);
        //修改人
        if(StringUtils.isNotEmpty(ServiceUtil.getUserId(request))){
            otherActionEntity.setUpdateUser(ServiceUtil.getUserId(request));
        }
        //修改时间
        otherActionEntity.setUpdateTime(now);
        //来源
        otherActionEntity.setCdSource(CvaConstants.CD_SOURCE);

        //必须有ID
        otherActionMapper.save(otherActionEntity);
    }

    @Override
    public void deleteById(String id) {
        QJgJgxwOtherActionEntity q = QJgJgxwOtherActionEntity.jgJgxwOtherActionEntity;
        jpaQueryFactory.update(q).where(q.id.eq(id)).set(q.actionCode, CvaConstants.DELETED_OPER_FLAG).execute();
    }
    /**
     *根据编码查询行为信息
     */
    @Override
    public rwjgLh findOneById(String actionId) {
        if(StringUtils.isEmpty(actionId)) {
            return null;
        }
        QJgJgxwOtherActionEntity q = QJgJgxwOtherActionEntity.jgJgxwOtherActionEntity;
        JgJgxwOtherActionEntity fetchOne = jpaQueryFactory
                .selectFrom(q)
                .where(q.id.eq(actionId),
                        q.cdOperation.eq(CvaConstants.UN_DELETED_FLAG)
                )
                .fetchOne();
        rwjgLh vo = new rwjgLh();

        if(fetchOne != null && StringUtils.isNotEmpty(fetchOne.getSuperviseObject()) ) {
            QJgLhjgJcdxEntity jcdx = QJgLhjgJcdxEntity.jgLhjgJcdxEntity;
            JgLhjgJcdxEntity dx = jpaQueryFactory
                    .selectFrom(jcdx)
                    .where(jcdx.enterprisecreditcode.eq(fetchOne.getSuperviseObject())
                    )
                    .fetchFirst();
            vo.setJgLhjgJcdxEntity(dx);
        }
        vo.setJgJgxwOtherActionEntity(fetchOne);

        return vo;
    }

    /**
     * 通过id查询其它行为信息
     * @param actionId
     * @return
     */
    @Override
    public JgJgxwOtherActionEntity findById(String actionId) {
        return otherActionMapper.findById(actionId).get();
    }

    /**
     * 物理删除其它行为信息
     * @param otherActionEntity
     */
    @Override
    public void physicalDelete(JgJgxwOtherActionEntity otherActionEntity) {
        try {
            otherActionMapper.delete(otherActionEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
